package com.cvit.cn.controller;

import java.io.InputStream;
import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cvit.cn.bean.Commodity;
import com.cvit.cn.bean.Msg;
import com.cvit.cn.service.CommodityService;

@Controller
public class CommodityController {

	@Autowired
	CommodityService commodityService;
	
	/**
	 * 根据releaseId和commenttype=1查询
	 * */
	@RequestMapping("/getReleaseId")
	@ResponseBody
	public Msg getReleaseId(@RequestParam("releaseId")String releaseId){
		List<Commodity> c=commodityService.getReleaseId(releaseId);
		if(c!=null){
		return Msg.success().add("c", c);
		}else{
			return Msg.fail().add("111", "失败");
		}
		
	}
	@RequestMapping("/getReleaseIds")
	@ResponseBody
	public Msg getReleaseIds(@RequestParam("releaseId")String releaseId){
		List<Commodity> c=commodityService.getReleaseIds(releaseId);
		if(c!=null){
		return Msg.success().add("c", c);
		}else{
			return Msg.fail().add("111", "失败");
		}
		
	}
	/*@RequestMapping("/releaseId")
	@ResponseBody
	public Msg releaseId(@RequestParam("releaseId")String releaseId){
		List<Commodity> cs=commodityService.releaseId();
		Commodity cc=new Commodity();
		cc.setReleaseId(releaseId);
		if(cs!=null){
		return Msg.success().add("cs", cs);
		}else{
			return Msg.fail().add("111", "失败");
		}
	}*/
	/**
	 * 根据commodityType=1查询
	 * */
	@RequestMapping("/getCommodityByType")
	@ResponseBody
	public Msg getCommodityByType(){
		List<Commodity> commoditys = commodityService.getCommodityByType();
		return Msg.success().add("commoditys", commoditys);
	}
	
	/**
	 * 查询全部
	 * */
	@ResponseBody
	@RequestMapping("/getComm")
	public Msg getComm(){
		List<Commodity> comm=commodityService.getAll();
		return Msg.success().add("comm", comm);
	} 
	/**
	 * 添加物品信息
	 * **/
	@RequestMapping("/addComm")
	@ResponseBody
	public Msg addComm(Commodity commodity){
		commodityService.addComm(commodity);
		return Msg.success();
	}
	/**
	 * 根据id删除
	 * */
	/*@RequestMapping("/deleCommId")
	@ResponseBody
	public Msg deleCommId(@RequestParam("id")String id){
		commodityService.deleCommId(id);
		return Msg.success();
	}*/
	/**
	 * 批量删除
	 * */
	@RequestMapping(value = "/deleCommId")
	@ResponseBody
	public Msg deleCommId(String id) {
		String[] ids = id.split(",");
		if (commodityService.deleCommId(ids) != 0) {
			return Msg.success().add("success", "删除物品成功");
		} else {
			return Msg.fail().add("fail", "删除物品失败");
		}
	}
	/**
	 * 按照release删除物品
	 * */
	@RequestMapping("/deleReleaseId")
	@ResponseBody
	public Msg deleReleaseId(@RequestParam("releaseId")String releaseId){
		commodityService.deleReleaseId(releaseId);
		return Msg.success();
	}
	
	/**
	 * 修改物品表
	 * */
	@ResponseBody
	@RequestMapping("/updataComm")
	public Msg updateComm(@RequestParam("id")String id,@RequestParam("commodityName")String commodityName,@RequestParam("commodityCategory")String commodityCategory,@RequestParam("specificationModel")String specificationModel,@RequestParam("company")String company,@RequestParam("number")String number) throws ParseException{
		//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		Commodity comm = new Commodity();
		comm.setId(id);
		comm.setCommodityName(commodityName);
		comm.setCommodityCategory(commodityCategory);
		comm.setSpecificationModel(specificationModel);
		comm.setCompany(company);
		comm.setNumber(number);
		commodityService.updataComm(comm);
		return Msg.success();
	}
	/**
	 * 把状态值都改成commodityType=0
	 * */
	@ResponseBody
	@RequestMapping("/updateType")
	public Msg updateType() throws ParseException{
		//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		Commodity comm = new Commodity();
		comm.setCommodityType("0");
		commodityService.updataType(comm);
		return Msg.success();
	}
	/**
	 * 根据ReleaseId给commodityTye赋值为1
	 * */
	@ResponseBody
	@RequestMapping("/updateReleaseId")
	public Msg updateReleaseId(@RequestParam("releaseId")String releaseId){
		Commodity comm = new Commodity();
		comm.setReleaseId(releaseId);
		comm.setCommodityType("1");
		commodityService.updataReleaseId(comm);
		return Msg.success();
	}
	/**
	 * excel表格导入
	 * */
	@RequestMapping(value="/addExcel",method=RequestMethod.POST)
	@ResponseBody
	public Msg importExcel(HttpServletRequest request,Commodity Commodity,String releaseId) throws Exception{
		//System.out.println("??????????"+orderId);
		//获取上传的文件 
	     MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
	     MultipartFile file = multipart.getFile("file"); 
	     InputStream in = file.getInputStream();
	     //数据导入  
	     Integer code = commodityService.importExcel(in,file,releaseId);
	     in.close();
	     if(code == 1){
	    	 return Msg.success().add("releaseId", "订单导入成功！"); 
	     }else{
	    	 return Msg.fail().add("code", "订单导入失败，单号输入有误！");
	     }
		
		
	}
	
	

}
